package com.qfedu.sem.view;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractXlsxView;

import com.qfedu.sem.dto.BugDto;

@Component
public class BugExportView extends AbstractXlsxView {
	private static Logger LOG = LogManager.getLogger(BugExportView.class);

	@Override
	protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 1.创建sheet页
		Sheet sh = workbook.createSheet("故障列表");
		// 2.创建标题行
		Row header = sh.createRow(0);
		Cell id = header.createCell(0, CellType.STRING);
		id.setCellValue("ID");
		Cell title = header.createCell(1, CellType.STRING);
		title.setCellValue("标题");
		Cell description = header.createCell(2, CellType.STRING);
		description.setCellValue("描述");
		Cell version = header.createCell(3, CellType.STRING);
		version.setCellValue("发现版本");
		Cell level = header.createCell(4, CellType.STRING);
		level.setCellValue("级别");
		Cell rate = header.createCell(5, CellType.STRING);
		rate.setCellValue("频度");
		Cell project = header.createCell(6, CellType.STRING);
		project.setCellValue("项目");
		Cell committer = header.createCell(7, CellType.STRING);
		committer.setCellValue("提交人");
		Cell fixer = header.createCell(8, CellType.STRING);
		fixer.setCellValue("修复人");
		Cell tester = header.createCell(9, CellType.STRING);
		tester.setCellValue("测试人");
		Cell commitTime = header.createCell(10, CellType.STRING);
		commitTime.setCellValue("提交时间");
		Cell currentNode = header.createCell(11, CellType.STRING);
		currentNode.setCellValue("当前节点");
		// 3.获取model中的数据
		List<BugDto> bugs = (List<BugDto>) model.get("bugs");
		// 4.创建数据行
		for (int i = 0; i < bugs.size(); i ++) {
			BugDto bug = bugs.get(i);
			Row row = sh.createRow(i + 1);
			id = row.createCell(0, CellType.STRING);
			id.setCellValue(bug.getId());
			title = row.createCell(1, CellType.STRING);
			title.setCellValue(bug.getTitle());
			description = row.createCell(2, CellType.STRING);
			description.setCellValue(bug.getDescription());
			version = row.createCell(3, CellType.STRING);
			version.setCellValue(bug.getVersionNo());
			level = row.createCell(4, CellType.STRING);
			level.setCellValue(bug.getLevel());
			rate = row.createCell(5, CellType.STRING);
			rate.setCellValue(bug.getRate());
			project = row.createCell(6, CellType.STRING);
			project.setCellValue(bug.getProjectName());
			committer = row.createCell(7, CellType.STRING);
			committer.setCellValue(bug.getCommitterName());
			fixer = row.createCell(8, CellType.STRING);
			fixer.setCellValue(bug.getFixerName());
			tester = row.createCell(9, CellType.STRING);
			tester.setCellValue(bug.getTesterName());
			commitTime = row.createCell(10, CellType.STRING);
			commitTime.setCellValue(bug.getCommitTime());
			currentNode = row.createCell(11, CellType.STRING);
			currentNode.setCellValue(bug.getCurrentNode());
		}
	}
}
